<template>
  <div class="Cangkushezhi">
    <div class="sTop">
      &nbsp;&nbsp;&nbsp;&nbsp;用户名：<el-input
        placeholder="请输入用户名"
        v-model="input"
        size="small"
        clearable
      ></el-input>
      &nbsp;<el-button
        type="primary"
        size="small"
        icon="el-icon-search"
        class="button1 detail"
        @click="searchUser"
        >查询</el-button
      >
      <el-button
        type="primary"
        size="small"
        icon="el-icon-refresh-right"
        class="button1 rePassword"
        @click="reload"
        >重置</el-button
      >
      <el-button
        type="primary"
        size="small"
        icon="el-icon-circle-plus-outline"
        class="button2 addUser"
        @click="centerDialogVisible2 = true"
        >添加用户</el-button
      >
    </div>

    <el-table
      :data="tableData"
      border
      stripe
      :row-style="{ height: '60px', padding: '0 0' }"
      :cell-style="{ height: '30px', padding: '0 0' }"
      style="width: 100%"
    >
      <el-table-column align="center" type="selection"> </el-table-column>
      <el-table-column prop="username" label="用户名" align="center" width="90">
      </el-table-column>
      <el-table-column
        prop="nickname"
        label="员工姓名"
        align="center"
        width="90"
      >
      </el-table-column>
      <el-table-column prop="sex" label="性别" align="center" width="90">
      </el-table-column>
      <el-table-column prop="partId" label="部门" align="center" width="110">
      </el-table-column>
      <el-table-column prop="fitstTime" label="入职时间" align="center">
      </el-table-column>
      <el-table-column prop="lastTime" label="离职时间" align="center">
        <template slot-scope="scope">
          {{
            scope.row.lastTime == '1970-01-01' ||
            scope.row.lastTime == 'NaN-0NaN-0NaN'
              ? ''
              : scope.row.lastTime
          }}
        </template>
      </el-table-column>
      <el-table-column prop="phoneNumber" label="手机号码" align="center">
      </el-table-column>
      <el-table-column prop="status" label="状态" align="center" width="90">
      </el-table-column>
      <el-table-column prop="address" label="家庭住址" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center" width="250">
        <template slot-scope="scope">
          <el-button
            class="addUser"
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
            >修改</el-button
          >
          <el-button
            class="rePassword"
            size="mini"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
          <el-button
            class="detail"
            size="mini"
            @click="handleChange(scope.$index, scope.row)"
            >{{
              tableData[scope.$index].status == '正常' ? '冻结' : '解冻'
            }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :page-size="pageShow"
      :current-page="pageNow"
      layout="prev, pager, next,sizes"
      :page-sizes="[2, 3, 5, 10]"
      @current-change="pageChange"
      @size-change="sizeChange"
      @prev-click="back()"
      @next-click="next()"
      :total="pageNumber"
    >
    </el-pagination>
    <!-- 添加新用户 -->
    <el-dialog
      title="添加新用户"
      :visible.sync="centerDialogVisible2"
      width="30%"
      center
    >
      <el-form
        :model="insertOneArr"
        ref="insertOneArr"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            type="username"
            v-model="insertOneArr.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="insertOneArr.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="员工姓名" prop="nickname">
          <el-input
            type="nickname"
            v-model="insertOneArr.nickname"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <div class="sexselect">
          <el-radio v-model="insertOneArr.sex" label="男">男</el-radio>
          <el-radio v-model="insertOneArr.sex" label="女">女</el-radio>
        </div>
        <el-form-item label="部门" prop="partId">
          <el-select v-model="insertOneArr.partId" placeholder="请选择">
            <el-option
              v-for="item in partArr"
              :key="item.partId"
              :label="item.partName"
              :value="item.partName"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <div class="datepicker">
          入职时间&nbsp;&nbsp;&nbsp;<el-date-picker
            v-model="insertOneArr.fitstTime"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </div>
        <div class="datepicker">
          离职时间&nbsp;&nbsp;&nbsp;<el-date-picker
            v-model="insertOneArr.lastTime"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </div>
        <el-form-item label="手机号码" prop="phoneNumber">
          <el-input
            type="phoneNumber"
            v-model.number="insertOneArr.phoneNumber"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="insertOneArr.status" placeholder="请选择用户状态">
            <el-option label="正常" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="家庭住址" prop="address">
          <el-input
            type="address"
            v-model="insertOneArr.address"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改用户信息 -->
    <el-dialog
      title="修改用户信息"
      :visible.sync="centerDialogVisible"
      width="30%"
      center
    >
      <el-form
        :model="managerInformation"
        ref="managerInformation"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            type="username"
            v-model="managerInformation.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="员工姓名" prop="nickname">
          <el-input
            type="nickname"
            v-model="managerInformation.nickname"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <div class="sexselect">
          <el-radio v-model="managerInformation.sex" label="男">男</el-radio>
          <el-radio v-model="managerInformation.sex" label="女">女</el-radio>
        </div>
        <el-form-item label="部门" prop="partId">
          <el-select v-model="managerInformation.partId" placeholder="请选择">
            <el-option
              v-for="item in partArr"
              :key="item.partId"
              :label="item.partName"
              :value="item.partName"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <div class="datepicker">
          入职时间&nbsp;&nbsp;&nbsp;<el-date-picker
            v-model="managerInformation.fitstTime"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </div>
        <div class="datepicker">
          离职时间&nbsp;&nbsp;&nbsp;<el-date-picker
            v-model="managerInformation.lastTime"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </div>
        <el-form-item label="手机号码" prop="phoneNumber">
          <el-input
            type="phoneNumber"
            v-model.number="managerInformation.phoneNumber"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="managerInformation.status"
            placeholder="请选择用户状态"
          >
            <el-option label="正常" value="1"></el-option>
            <el-option label="禁用" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="家庭住址" prop="address">
          <el-input
            type="address"
            v-model="managerInformation.address"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 删除用户对话框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <i class="el-icon-warning"></i><span>确认删除此用户吗</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteManager">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 改变用户状态对话框 -->
    <el-dialog title="提示" :visible.sync="centerDialogVisible3" width="30%">
      <span
        ><i type="waring" class="el-icon-warning"></i>确认修改用户状态吗</span
      >
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible3 = false">取 消</el-button>
        <el-button type="primary" @click="changeStatus">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import yuangong from '@/api/yuangong.js';
import util from '../../utils/utils';
export default {
  data() {
    return {
      userStatus: '', //当前点击用户的状态status
      input: '', //搜索用
      Userid: '', //删除用的用户id
      pageNow: 1 /* 当前页数 */,
      pageNumber: 50 /* 总页数 */,
      pageShow: 10 /* 每页显示个数 */,
      centerDialogVisible: false,
      dialogVisible: false,
      centerDialogVisible2: false,
      centerDialogVisible3: false,
      partArr: [], //部门数组
      managerArr: [], //修改用数组
      insertOneArr: {
        username: '',
        password: '',
        nickname: '',
        partId: '',
        fitstTime: '',
        lastTime: '',
        phoneNumber: '',
        address: ''
      }, //添加用对象
      tableData: [], //渲染到表格的数据
      options: [
        {
          //select 选项
          value: '1',
          label: '自家仓库'
        },
        {
          value: '2',
          label: '电力公司仓库'
        }
      ],
      value: '', //select当前选定的 option
      managerInformation: {}, //用于修改
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  created() {
    //获取部门列表
    yuangong.getAllPart().then(res => {
      this.partArr = res.data.data.list;
    });
    //获取用户列表
    yuangong.getYuangong(this.pageNow, this.pageShow).then(res => {
      /* console.log(res); */
      this.managerArr = res.data.data.list;
      this.pageNumber = res.data.data.total;
      this.createdShift(this.managerArr);
      this.tableData = this.managerArr;
    });
  },
  methods: {
    //pageSize改变
    sizeChange(i) {
      this.pageShow = i;
      //获取用户列表
      yuangong.getYuangong(this.pageNow, this.pageShow).then(res => {
        /* console.log(res); */
        this.managerArr = res.data.data.list;
        this.pageNumber = res.data.data.total;
        this.createdShift(this.managerArr);
        this.tableData = this.managerArr;
      });
    },
    //点击页码
    pageChange(i) {
      this.pageNow = i;
      //获取用户列表
      yuangong.getYuangong(this.pageNow, this.pageShow).then(res => {
        /* console.log(res); */
        this.managerArr = res.data.data.list;
        this.pageNumber = res.data.data.total;
        this.createdShift(this.managerArr);
        this.tableData = this.managerArr;
      });
    },
    //上一页
    back() {
      this.pageNow--;
    },
    //下一页
    next() {
      this.pageNow++;
    },
    /* 修改 */
    handleEdit(i, v) {
      this.centerDialogVisible = true;
      this.managerInformation = v;
    },

    /*修改更新数据*/
    update() {
      this.centerDialogVisible = false;
      if (this.managerInformation.sex == '男') {
        this.managerInformation.sex = '0';
      } else {
        this.managerInformation.sex = '1';
      }
      if (this.managerInformation.status == '正常') {
        this.managerInformation.status = '1';
      } else {
        this.managerInformation.status = '0';
      }
      for (let i = 0; i < this.partArr.length; i++) {
        if (this.managerInformation.partId == this.partArr[i].partName) {
          this.managerInformation.partId = this.partArr[i].partId;
        }
      }
      yuangong.updateUser(this.managerInformation).then(res => {
        console.log(res);
        location.reload();
      });
    },
    /*删除管理员*/

    handleDelete(i, v) {
      this.dialogVisible = true;
      this.Userid = v.id;
    },
    /*删除管理员*/
    deleteManager() {
      this.dialogVisible = false;
      yuangong.deleteUser(this.Userid).then(res => {
        location.reload();
      });
    },
    //冻结 启用 用户
    handleChange(i, v) {
      this.centerDialogVisible3 = true;
      this.userStatus = v.id;
    },
    //冻结 启用 用户
    changeStatus() {
      yuangong.changeStatus(this.userStatus).then(res => {
        /* console.log(res); */
        location.reload();
      });
      this.centerDialogVisible3 = false;
    },
    //添加用户
    addUser() {
      this.centerDialogVisible2 = false;
      if (this.insertOneArr.sex == '男') {
        this.insertOneArr.sex = '0';
      } else {
        this.insertOneArr.sex = '1';
      }
      if (this.insertOneArr.status == '正常') {
        this.insertOneArr.status = '1';
      } else {
        this.insertOneArr.status = '0';
      }
      for (let i = 0; i < this.partArr.length; i++) {
        if (this.insertOneArr.partId == this.partArr[i].partName) {
          this.insertOneArr.partId = this.partArr[i].partId;
        }
      }
      yuangong.addUser(this.insertOneArr).then(res => {
        /* console.log(res); */
        location.reload();
      });
    },
    //created渲染表格时转换状态和性别
    createdShift(arr) {
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].status == '1') {
          arr[i].status = '正常';
        } else {
          arr[i].status = '禁用';
        }
        if (arr[i].sex == '0') {
          arr[i].sex = '男';
        } else {
          arr[i].sex = '女';
        }

        //ajax 获取部门名
        let ppartId = arr[i].partId;
        yuangong.getPartName(ppartId).then(res => {
          /* console.log(res); */
          arr[i].partId = res.data.data.usersPart.partName;
        });

        let ftimes = util.shiftDate(arr[i].fitstTime);
        let ltimes = util.shiftDate(arr[i].lastTime);

        arr[i].fitstTime = ftimes;
        arr[i].lastTime = ltimes;
      }
    },
    //转换状态和性别
    shiftStaSex(arr) {
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].status == '1') {
          arr[i].status = '正常';
        } else {
          arr[i].status = '禁用';
        }
        if (arr[i].sex == '0') {
          arr[i].sex = '男';
        } else {
          arr[i].sex = '女';
        }
      }
    },
    //搜索用户
    searchUser() {
      yuangong.searchUser(1, 10, this.input).then(res => {
        /* console.log(res); */
        this.tableData = res.data.data.list;
        this.createdShift(this.tableData);
        this.pageNumber = res.data.data.total;
        /* console.log(this.tableData); */
      });
    },
    //重置
    reload() {
      //获取用户列表
      yuangong.getYuangong(this.pageNow, this.pageShow).then(res => {
        /* console.log(res); */
        this.managerArr = res.data.data.list;
        this.pageNumber = res.data.data.total;
        this.createdShift(this.managerArr);
        this.tableData = this.managerArr;

        this.input = '';
      });
    }
  }
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}
.Cangkushezhi {
  background-color: white;
}
.el-input {
  width: 200px;
  margin-right: 10px;
}
.sTop {
  padding-top: 20px;
  font-size: 14px;
  margin-bottom: 40px;
}
.el-table thead {
  background-color: #f5f7fa;
}
.has-gutter .is-leaf .cell {
  text-align: center;
  padding-left: 10px;
  width: 150px;
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-icon-warning {
  color: orange;
  font-size: 20px;
  margin-right: 5px;
}

.sTop >>> .button1 {
  text-align: center;
  width: 78px;
  height: 32px;
}
.sTop >>> .button2 {
  text-align: center;
  width: 118px;
  height: 32px;
}
.sTop >>> .button1,
.button2 {
  font-size: 14px;
}
.is-plain {
  width: 80px;
}
.dialog-footer .el-button--default,
.el-button--primary {
  width: 74px;
  height: 40px;
}
.datepicker {
  height: 40px;
  width: 405px;
  margin-bottom: 20px;
  padding-left: 30px;
}
.sexselect {
  width: 400px;
  height: 40px;
  padding-left: 100px;
}
.datepicker .el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 200px;
}
.el-select >>> .el-input {
  width: 200px;
}
</style>
